<?php


namespace Org\WeChat;


/**
 * 微信公众号的工具类
 * Class WeChatUtils
 * @package Org\WeChat
 */
class WeChatUtils
{

    /**
     *
     * 获取临时的二维码数据
     * @param array $params         一维数组
     * @return string               构建后的带参数的二维码数据
     */
    public static function getQrcodeTempData($params)
    {
        $params = self::__getParams($params);
        $data = '{"expire_seconds": 604800, "action_name": "QR_STR_SCENE", "action_info": {"scene": {"scene_str": "'.$params.'"}}}';
        return $data;
    }


    /**
     * 构建参数
     * @param array $data           数组
     * @return string               构建后的字符串
     */
    private static function __getParams($data)
    {
        if (empty($data)) {
            return '';
        }

        $result = '{';
        $count = 1;
        $len = count($data);
        foreach ($data as $k => $v) {

            $result .= "\\\"{$k}\\\":\\\"{$v}\\\"";
            if ($count < $len) {
                $result .= ",";
            }

            $count++;
        }

        $result .= '}';
        return $result;
    }

}